package routes

import (
	v1 "ginblog/api/v1"
	"ginblog/middleware"
	"ginblog/utils"
	"github.com/gin-gonic/gin"
)

func InitRouter() {
	gin.SetMode(utils.AppMode)
	//Default相对于New，多了两个初始的中间件，因为自己要日志，所以用new，提升个性化能力
	r := gin.New()
	r.Use(gin.Recovery())
	r.Use(middleware.Logger())
	r.Use(middleware.Cors())

	//托管的步骤
	r.LoadHTMLGlob("static/admin/index.html")
	r.Static("admin/static", "static/admin/static")
	r.StaticFile("admin/favicon.ico", "static/admin/favicon.ico")
	r.GET("admin", func(c *gin.Context) {
		c.HTML(200, "index.html", nil)
	})

	//router := r.Group("api/v1")
	//{
	//	router.GET("hello", func(c *gin.Context) {
	//		c.JSON(http.StatusOK,gin.H{
	//			"msg":"ok",
	//		})
	//	})
	//}

	auth := r.Group("api/v1")
	auth.Use(middleware.JwtToken())
	{
		//用户模块的路由接口
		auth.POST("user/add", v1.AddUser)
		auth.PUT("user/:id", v1.UpdateUser)
		auth.DELETE("user/:id", v1.DeleteUser)
		//分类模块的路由接口
		auth.POST("category/add", v1.AddCate)
		auth.PUT("category/:id", v1.UpdateCate)
		auth.DELETE("category/:id", v1.DeleteCate)
		//文章模块的路由接口
		auth.POST("article/add", v1.AddArticle)
		auth.PUT("article/:id", v1.UpdateAticle)
		auth.DELETE("article/:id", v1.DeleteArticle)
		//上传文件
		auth.POST("upload", v1.UpLoad)

	}
	router := r.Group("api/v1")
	{
		router.GET("users", v1.GetUserList)
		router.GET("user/:id", v1.GetOneUser)
		router.GET("category", v1.GetCateList)
		router.GET("category/:id", v1.GetOneCate)
		router.GET("article", v1.GetArticleList)
		router.GET("article/:id", v1.GetArticleInfo)
		router.GET("articleByCate/:id", v1.GetArtByCate)
		router.POST("login", v1.Login)
	}
	r.Run(utils.HttpPort)
}
